/**
 * Created by z on 2016/11/10.
 */
;(function($,window,document,undefined){

  $('#JQ_create_01').on('click',function() {
    location.href = docApi.url_com_add_update_html();
  });

  var $pipe = $('#JQ_pipeline').select3();
  var $segment = $('#JQ_segment').select3();
  var $doc_table= $('#JQ_datatable_01');
  var $starttime=$('#JQ_power_startDate');
  var $endtime=$('#JQ_power_endDate');
  var $type=$("#type");

  /********筛选对象 start**********/
    // 触发管道
  $pipe.element.on('select',function(e,v){
    var id = v || $pipeline.val();
    if(id){
      // 管段
      DyyBase.ajax(DyyBase.url_segment(), {pipeLineId:id}, function(dataset){
        var json = DyyBase.getSelect(dataset);
        $segment.add(json);
      });
    }else{
      $segment.add([]);
    }
  });
  //查询参数
  var filter = {
    getDocFilter: function (data) {
      var filter = {};
      filter.mode = true;
      // 分页参数
      if (data) {
        if (data.start == 0) {
          filter['jsonBean.pageNum'] = 1;
        } else {
          filter['jsonBean.pageNum'] = data.start / 10 + 1;
        }
        filter['jsonBean.pageCount'] = data.length;
      }
      // 查询参数
      filter.pipeLineId = function () {
        return $pipe.val();
      }();
      filter.segmentId = function () {
        return $segment.val();
      }();
      filter.beginDate = function () {
        return $starttime.val();
      }();
      filter.endDate = function () {
        var v1=$starttime.val();
        var v2=$endtime.val();
        if(v2){
          if(v1 && !Dyy.checkDateLate(v1,v2)){
            $endtime.TIP({content:'结束时间应晚于开始时间'})
            filter.mode=false;
          }
          return v2;
        }
        return '';
      }();
      filter.type=function(){
        return $type.val();
      }();
      return filter;
    }
  };
  var $tabl = $('#JQ_datatable_01');

  //加载表格
  var DT01 = $tabl.DataTable({
    'language': {
      'paginate': {
        'next': '下一页',
        'previous': '上一页'
      },
      'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
      'zeroRecords':DyyBase.table_empty(),
      'infoEmpty': ''
    },
    'autoWidth': false,
    'order': [],
    'serverSide': true,
    'pageLength': 10,
    'ordering': false,
    'processing': false,
    'searching': false,
    'lengthChange': false,
    'ajax': function (data, callback) {
      var param = filter.getDocFilter(data);
      DyyBase.ajax(docApi.url_pipe_search(), param, function (dataset) {
        var returnData = {};
        returnData.draw = data.draw;
        returnData.recordsTotal = dataset.result.total;
        returnData.recordsFiltered = dataset.result.total;
        returnData.data = DyyBase.clearNull(dataset.result.list);
        callback(returnData);
      }, {traditional : true});
    },
    'drawCallback': function () {
    },
    'columns': [
      {
        data: 'pipeLineName', render: function (data) {
        return '<span title="' + data + '">' + data + '</span>';
      }
      },
      {
        data: 'segmentName', render: function (data) {
        return '<span title="' + data + '">' + data + '</span>';
      }
      },
      {
        data: 'name', render: function (data) {
        return '<span title="' + data + '">' + data + '</span>';
      }
      },
      {
        data: 'date', render: function (data) {
        return '<span title="' + data + '">' + data + '</span>';
      }
      },
      {
        data: 'id', render: function (data,type, row) {
        return '<i title="下载" class="zy-file-download-icon" data-id="' + data + '" data-name="'+row.name+'"></i>';
      }
      },
      {
        data: 'id', render: function (data) {
        var url =  docApi.url_com_add_update_html();
        return '<a href="' + url + '?id=' + data + '"><i title="编辑" class="zy-record-add-icon" data-id="' + data + '"></a>';
      }
      },
      {
        data: 'id', render: function (data) {
        return '<i title="删除" class="zy-delete-upload-icon " data-id="' + data + '"></i>';
      }
      }
    ]
  });

  //查询
  $('#JQ_search').click(function () {
      var param = filter.getDocFilter();
      if(param.mode) {
          if (DT01) {
              DT01.draw();
          }
      }
  });

  //删除单行
  $tabl.on('click','i.zy-delete-upload-icon', function (event) {
    var $this = $(this);
    var id = $this.attr('data-id');
    $this.MSG({
      type: 'confirm',
      height: 230,
      content: '您确认要删除吗?',
      yesBtnTxt: '确认',
      yesBtnAct: function () {
        DyyBase.ajax(docApi.url_com_deleted(), {id: id}, function (dataset) {
          if (dataset.bool) {
            $this.MSG({
              type: 'success',
              content: dataset.result,
              yesBtnAct: function () {
                if (DT01) {
                  DT01.draw();
                }
              }
            });
          }
        });
      }
    });
    event.stopPropagation();
    return false;
  });

  // 下载报告
  $doc_table.on('click', 'i.zy-file-download-icon', function(){
    var $this = $(this);
    var $form = $('#JQ_download_form').empty();
    var $iframe = $('#JQ_download_iframe').empty();
    var postObj = {
      businessId: $this.attr('data-id'),
      downFileName: $this.attr('data-name'),
      type: 'docPipeLine',
      ymd : true
    };
    for(var k in postObj){
      var $input = $('<input type="hidden">');
      $input.prop('name', k).val(postObj[k]);
      $form.append($input);
    }
    $form.submit();
    $iframe.get(0).onload = function(){
      try{
        var content = this.contentWindow.document.body.innerHTML;
        content = $.parseJSON(content);
        if(content.bool){

        }else{
          $this.MSG({
            type: 'error',
            content: content.result
          });
        }
      }catch(e){
        $this.MSG({
          type: 'error',
          content: e.message
        });
      }
    }
  });

})(jQuery,window,document);